package com.lovo.pis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.pis.dto.CustomBean;
import com.lovo.pis.dto.PageBean;
import com.lovo.pis.dto.SupplierBean;
import com.lovo.pis.dto.UserBean;
import com.lovo.pis.mapper.CustomMapper;
import com.lovo.pis.mapper.SupplierMapper;
import com.lovo.pis.service.ICustomService;
import com.lovo.pis.service.ISupplierService;
import com.lovo.pis.utils.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class CustomServiceImpl extends ServiceImpl<CustomMapper, CustomBean> implements ICustomService {

    @Autowired
    CustomMapper customMapper;

    @Override
    public IPage<CustomBean> queryByItem(PageBean<CustomBean> pageInfo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (pageInfo.getData().getName() != null && pageInfo.getData().getName() != "") {
            queryWrapper.like("name", pageInfo.getData().getName());
        }
        if (pageInfo.getData().getScope() != null && pageInfo.getData().getScope() != "") {
            queryWrapper.like("scope", pageInfo.getData().getScope());
        }
        return customMapper.selectPage(new Page<>(pageInfo.getCurPage(), pageInfo.getPageSize()), queryWrapper);
    }

    @Override
    public CustomBean queryById(int id) {
        return customMapper.selectById(id);
    }

    @Override
    public void changeById(CustomBean supplierBean) {
        customMapper.updateById(supplierBean);
    }

    @Override
    public void addSupplier(CustomBean supplierBean) {
        customMapper.insert(supplierBean);
    }

    @Override
    public List<CustomBean> queryAllName() {
        return customMapper.selectList(null);
    }

    @Override
    public void sendTaskEmail() throws Exception {
        List<UserBean> receiverList = customMapper.selectGeTenThousand(10000, LocalDate.now());
        if (receiverList.size() > 0) {
            for (UserBean userBean : receiverList) {
                String sender = "进销存系统";
                String subject = "感谢您的支持!";
                String content = """
                        贵公司购买的商品订单金额超过10000元！
                        感谢您对进销存系统的支持！！
                        """;
                EmailUtil.sendEmail(userBean, subject, content, sender);
            }
        }
    }


}
